﻿Imports System.Net
Imports System.Net.WebClient
Imports System.IO
Public Class Form2
    Private WithEvents variabile As WebClient
    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If IO.File.Exists("news.txt") Then
            Form6.Show()
        End If
        Label4.Text = "Azione: Connessione al server"
        If My.Computer.Network.IsAvailable = True Then
            ProgressBar1.Value = 10
            Label4.Text = "Azione: Ricerca aggiornamenti in corso"
            variabile = New WebClient
            variabile.DownloadFileAsync(New Uri("http://rinnegatamante.netsons.org/TACG/7.8/patch.rar"), "patch.rar")
        Else
            Label4.Text = "Azione: Impossibile collegarsi al server"
            ProgressBar1.Value = 100
            Button1.Enabled = True
        End If
    End Sub
    Private Sub variabile_DownloadFileCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs) Handles variabile.DownloadFileCompleted
        Dim PatchSize As Integer = Math.Floor(New FileInfo("patch.rar").Length)
        If PatchSize = 0 Then
            ProgressBar1.Value = 100
            Label4.Text = "Azione: Non esistono nuovi aggiornamenti!"
            My.Computer.FileSystem.DeleteFile("patch.rar")
            Button1.Enabled = True
        Else
            Label4.Text = "Azione: Decompressione del file in corso"
            Dim esd = Process.Start("patching.bat")
            ProgressBar1.Increment(20)
            esd.WaitForExit()
            MsgBox("Per rendere effettive le modifiche è necessario riavviare il tool. Riavviare?", MsgBoxStyle.YesNo, "Aggiornamento")
            If MsgBoxResult.Ok Then
                Shell("newpatching.exe", AppWinStyle.Hide)
                Me.Close()
            End If
        End If
    End Sub
    Private Sub variabile_DownloadProgressChanged(ByVal sender As Object, ByVal e As System.Net.DownloadProgressChangedEventArgs) Handles variabile.DownloadProgressChanged
        ProgressBar1.Value = 10 + Math.Floor(e.ProgressPercentage / 2)

        If Math.Floor(e.TotalBytesToReceive / 1024) < 2000 Then
            Label3.Text = "Download: " & Math.Floor(e.BytesReceived / 1024) & " KB / " & Math.Floor(e.TotalBytesToReceive / 1024) & " KB"
            Label4.Text = "Azione: Download in corso. Completato al " & e.ProgressPercentage & "%"
        Else
            Dim Value1old As String
            Dim Value1 As String
            Dim Value2old As String
            Dim Value2 As String
            If (e.BytesReceived / 1024 / 1024) < 1000 Then
                Value1old = (e.BytesReceived / 1024 / 1024)
                Value1 = Value1old.Substring(0, 6)
                If (e.BytesReceived / 1024 / 1024) < 100 Then
                    Value1old = (e.BytesReceived / 1024 / 1024)
                    Value1 = Value1old.Substring(0, 5)
                    If (e.BytesReceived / 1024 / 1024) < 10 Then
                        Value1old = (e.BytesReceived / 1024 / 1024)
                        Value1 = Value1old.Substring(0, 4)
                    End If
                End If
            End If
            If (e.TotalBytesToReceive / 1024 / 1024) < 1000 Then
                Value2old = (e.TotalBytesToReceive / 1024 / 1024)
                Value2 = Value2old.Substring(0, 6)
                If (e.TotalBytesToReceive / 1024 / 1024) < 100 Then
                    Value2old = (e.TotalBytesToReceive / 1024 / 1024)
                    Value2 = Value2old.Substring(0, 5)
                    If (e.TotalBytesToReceive / 1024 / 1024) < 10 Then
                        Value2old = (e.TotalBytesToReceive / 1024 / 1024)
                        Value2 = Value2old.Substring(0, 4)
                    End If
                End If
            End If
            Label3.Text = "Download: " & Value1 & " MB / " & Value2 & " MB"
            Label4.Text = "Azione: Download in corso. Completato al " & e.ProgressPercentage & "%"
        End If
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Form1.Show()
        Me.Close()
    End Sub
End Class